/* Copyright ©, 2022, ECARX (Hubei) Technology Co., Ltd. All rights reserved.
 *
 * NOTICE: This file contains information which is confidential and proprietary to ECARX
 * (Hubei) Technology Co., Ltd. and/or other developers. No license to any intellectual
 * property rights contained within this file is either granted or implied except as may be
 * provided in an agreement with ECARX (Hubei) Technology Co., Ltd.
 *
 * Any unauthorized copying or distribution of content from this file in whole or in part
 * is prohibited.
 */
#include "PlatformProperty.h"

using Property = ecarx::vhal::core::property::Property;

#define SEND_RX_OFF  0
#define SEND_RX_ON  1

enum HardKeySettings: uint32_t {
    SWT_KEY_OFF,                   //SWT按键背光关闭
    SWT_KEY_ON,                    //按键SWT背光开启
};

static sdb::UsgModSts1 mLastUsgMode = sdb::UsgModSts1::UsgModAbdnd;
static sdb::UsgModSts1 mUsgMode = sdb::UsgModSts1::UsgModAbdnd;
static sdb::CarModSts1 mCarMode = sdb::CarModSts1::CarModNorm;
static sdb::TwliBriSts1 mTwlibri = sdb::TwliBriSts1::Night;
static int32_t mSaveBattery = lcfg::LocalConfig::getInstance().getInt<int32_t>(lcfg::LCFG_SaveBattery);
static int32_t mActvnOfSwtIllmnCenRx = -1;


//设定DMSM/RCSM/SWSM key
static void setHardKeySwtType(Property& property) {
    VHALOG.d("SETTING_KEY_BACKLIGHT::%s mUsgMode=%d, mLastUsgMode=%d, mCarMode=%d, mTwlibri=%d",
        __func__, mUsgMode, mLastUsgMode, mCarMode, mTwlibri);
    property.removeMsg(SWT_KEY_ON);
    property.removeMsg(SWT_KEY_OFF);
    if (mCarMode != sdb::CarModSts1::CarModTrnsp && mCarMode != sdb::CarModSts1::CarModFcy) {
        // usage是abandoned状态
        if (mUsgMode == sdb::UsgModSts1::UsgModAbdnd) {
            property.sendMsg(SWT_KEY_OFF, 0);
        } else if (mUsgMode == sdb::UsgModSts1::UsgModInActv) {
            //判断是usageMode是从哪种状态迁移过来的
            if ((mLastUsgMode == sdb::UsgModSts1::UsgModActv) ||
                (mLastUsgMode == sdb::UsgModSts1::UsgModDrvg) ||
                (mLastUsgMode == sdb::UsgModSts1::UsgModCnvinc)) {
                property.sendMsg(SWT_KEY_ON, 0);
                property.sendMsg(SWT_KEY_OFF, mSaveBattery * 60 * 1000);
            } else {
                //判断日夜模式
                if (mTwlibri == sdb::TwliBriSts1::Night) {
                    property.sendMsg(SWT_KEY_ON, 0);
                } else {
                    property.sendMsg(SWT_KEY_OFF, 0);
                }
            }
        } else {
            property.sendMsg(SWT_KEY_ON, 0);
        }
    } else {
        property.sendMsg(SWT_KEY_OFF, 0);
    }
}

//[SW_FC_005_002] 按键亮度调节
VIRTUAL_PROPERTY(VIRTUAL_SETTING_KEY_BACKLIGHT_STATE) {
    builder.toTask().subscribeSignals(
    [this]() {
        auto usgMode = SIGNAL.getVehModMngtGlbSafe1UsgModSts();
        VHALOG.d("SETTING_KEY_BACKLIGHT::%s usgMode=%d, mUsgMode=%d, mLastUsgMode=%d", __func__, usgMode, mUsgMode, mLastUsgMode);
        if (usgMode != mUsgMode) {
            mUsgMode = usgMode;
            setHardKeySwtType(*this);
            mLastUsgMode = usgMode;
        }
    }, SignalRX::VehModMngtGlbSafe1UsgModSts)
    //subcscribe twlibri
    .subscribeSignals(
        [this]() {
        auto twlibri = SIGNAL.getTwliBriSts();
        VHALOG.d("SETTING_KEY_BACKLIGHT::%s twlibri=%d, mTwlibri=%d", __func__, twlibri, mTwlibri);
        if (mTwlibri != twlibri) {
            mTwlibri = twlibri;
            setHardKeySwtType(*this);
        }
    }, SignalRX::TwliBriSts)
    //subcscribe carmode
    .subscribeSignals(
        [this]() {
        auto carMode = SIGNAL.getVehModMngtGlbSafe1CarModSts1();
        VHALOG.d("SETTING_KEY_BACKLIGHT::%s carMode=%d, mCarMode=%d", __func__, carMode, mCarMode);
        if (mCarMode != carMode) {
            mCarMode = carMode;
            setHardKeySwtType(*this);
        }
    }, SignalRX::VehModMngtGlbSafe1CarModSts1)
    .onMsgEvent(SWT_KEY_OFF,
        []() {
        VHALOG.d("SETTING_KEY_BACKLIGHT::%s, MsgId SWT_KEY_OFF", __func__);
        if (mActvnOfSwtIllmnCenRx != SEND_RX_OFF) {
            SIGNAL.setActvnOfSwtIllmnCen(sdb::Boolean::FALSE);
            mActvnOfSwtIllmnCenRx = SEND_RX_OFF;
        }
    })
    .onMsgEvent(SWT_KEY_ON,
        []() {
        VHALOG.d("SETTING_KEY_BACKLIGHT::%s, MsgId SWT_KEY_ON", __func__);
        if (mActvnOfSwtIllmnCenRx != SEND_RX_ON) {
            SIGNAL.setActvnOfSwtIllmnCen(sdb::Boolean::TRUE);
            mActvnOfSwtIllmnCenRx = SEND_RX_ON;
        }
    });

}
